//
//  YMBaseTabBarViewController.m
//  PCR
//
//  Created by shumei on 16/6/3.
//  Copyright © 2016年 shumei. All rights reserved.
//

#import "YMBaseTabBarViewController.h"
#import "AppDelegate.h"
@interface YMBaseTabBarViewController ()<UIScrollViewDelegate>

@end

@implementation YMBaseTabBarViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self.tabBar setTranslucent:YES];
//    [self.tabBar removeFromSuperview];
    
    [self initTabPage];
    [self _initMyTabBar];
    
}


- (void)initTabPage
{
    _scroll_View =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, MainSCreen.width, MainSCreen.height )];
    _scroll_View.delegate                       = self;
    _scroll_View.pagingEnabled                  = YES;
    _scroll_View.showsHorizontalScrollIndicator = NO;
    _scroll_View.showsVerticalScrollIndicator   = NO;
    _scroll_View.backgroundColor=[UIColor whiteColor];
    _scroll_View.contentSize                    = CGSizeMake(_contorll_array.count * MainSCreen.width, MainSCreen.height );
    [self.view addSubview:_scroll_View];
    
    for (int x = 0; x < _contorll_array.count; x ++)
    {
        YMBaseNavViewController * nav = _contorll_array[x];
        nav.view.frame                = CGRectMake(x * MainSCreen.width, 0, MainSCreen.width, MainSCreen.height);
        [_scroll_View addSubview:nav.view];
    }
    
    [self.view bringSubviewToFront:_YMTabBar];
    
}

- (void)_initMyTabBar
{
    if (!_YMTabBar)
    {
        _YMTabBar = [[UIView alloc]initWithFrame:(CGRect){zero,MainSCreen.height - tabBar_height,MainSCreen.width,tabBar_height}];
    }
    [_YMTabBar setBackgroundColor:[YMTool colorWithHexString:main_color]];
    [self.view addSubview:_YMTabBar];
    [self setMyTabBar];
    
}
- (void)setMyTabBar
{
    NSArray * images_nol = @[@"homepage_normal.png",@"task_Progress_normal.png",@"Indiana_records_normal.png",@"mine_normal.png"];
    NSArray * images_sel = @[@"homepage_highLight.png",@"task_Progress_highLight.png",@"Indiana-records.png",@"mine_highLight.png"];
    NSArray * titles     = @[@"主页",@"任务列表",@"任务进展",@"我的"];
    
    
    for (int i = 0; i < titles.count; i ++) {
        UIButton *bt = [[UIButton alloc] initWithFrame:(CGRect){MainSCreen.width / titles.count * i,zero,MainSCreen.width / titles.count,tabBar_height}];
        [bt setImage:[UIImage imageNamed:images_nol[i]]
            forState:UIControlStateNormal];
        [bt setImage:[UIImage imageNamed:images_sel[i]]
            forState:UIControlStateHighlighted];
        [bt setImage:[UIImage imageNamed:images_sel[i]]
            forState:UIControlStateSelected];
        [bt setTitle:titles[i]
            forState:UIControlStateNormal];
        [bt setTitleColor:[UIColor orangeColor]
                 forState:UIControlStateSelected];
        [bt setTitleColor:[UIColor orangeColor]
                 forState:UIControlStateHighlighted];
        [bt setTitleColor:[UIColor blackColor]
                 forState:UIControlStateNormal];
        bt.titleLabel.alpha       = 0.4;
        bt.tag                    = i + comTag;
        bt.userInteractionEnabled = YES;
        [bt addTarget:self
               action:@selector(tabBarBtAction:)
     forControlEvents:UIControlEventTouchUpInside];
        bt.titleLabel.font        = kFont(12);
        if (i == 0)
        {
            bt.selected           = YES;//默放选中第一个
            bt.titleLabel.alpha   = 1;

            UIView *lineV         = [[UIView alloc] initWithFrame:CGRectMake(zero, zero, MainSCreen.width, 0.5)];
            lineV.backgroundColor = [UIColor lightGrayColor];
            lineV.alpha           = 0.4;
            [_YMTabBar addSubview:lineV];
        }

        CGSize titleSize          = [titles[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, tabBar_height)
                                                            options:NSStringDrawingUsesLineFragmentOrigin
                                                         attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}
                                                            context:nil].size;
        CGSize imageSize          = [UIImage imageNamed:images_sel[i]].size;


        [bt setTitleEdgeInsets:(UIEdgeInsets){tabBar_height / 2,- imageSize.width,0,0}];

        [bt setImageEdgeInsets:(UIEdgeInsets){0,0,tabBar_height / 2 - 10,-titleSize.width}];

        [_YMTabBar addSubview:bt];
        
    }
    
    
    

    
    
    
    
    
    //圆弧
    //    UIView *circle = [[UIView alloc]initWithFrame:(CGRect){(k_s_w - (k_s_w / 5)) / 2,- (k_s_w / 20) ,k_s_w / 5,k_s_w / 5}];
    //
    //    [circle setBackgroundColor:[JWTool colorWithHexString:main_color]];
    //
    //    circle.layer.cornerRadius = (k_s_w / 5)/ 2.0f;
    //
    //    [self.jwTabBar insertSubview:circle atIndex:0];
    
    
    
}
- (void)tabBarBtAction:(UIButton *)sender
{
    for (UIButton *bt in _YMTabBar.subviews) {
        if ([bt isKindOfClass:[UIButton class]]) {
            
            bt.selected         = NO;
            bt.highlighted      = NO;
            bt.titleLabel.alpha = 0.4;
            [bt setUserInteractionEnabled:YES];
        }
    }
    
    sender.selected         = YES;
    sender.titleLabel.alpha = 1;//选中时为1
    [sender setUserInteractionEnabled:NO];

    
//    NSInteger tag           = sender.tag;
//
//    self.selectedIndex      = tag - comTag;
    
    //通过按钮的点击改变视图的界面
    _scroll_View.contentOffset = CGPointMake((sender.tag - comTag) * MainSCreen.width, 0);
    
    if (self.TabBarBtnAction)
    {
        
        self.TabBarBtnAction(sender);
    }
    
}



-(void)actionWithTabBarButtonBlock:(void(^)(UIButton * button))TabBarBtnHandler
{
    self.TabBarBtnAction = TabBarBtnHandler;
}



- (void)setTabbarHidden
{
//    [UIView animateWithDuration:0.35 animations:^{
//    _YMTabBar.frame = CGRectMake(-1000, _YMTabBar.frame.origin.y, MainSCreen.width, _YMTabBar.frame.size.height);
    _YMTabBar.hidden   = YES;
//    self.tabBar.hidden = YES;
//    }];
    

}

- (void)setTabbarShow
{
    //    [UIView animateWithDuration:0.35 animations:^{
    //    }];
    _YMTabBar.hidden   = NO;
//    self.tabBar.hidden = NO;
}

//单例对象
static YMBaseTabBarViewController *_YMTabBarVC = nil;
+(instancetype)shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _YMTabBarVC = [[super allocWithZone:NULL] init] ;
    }) ;
    
    return _YMTabBarVC ;
}

+(id)allocWithZone:(struct _NSZone *)zone
{
    return [YMBaseTabBarViewController shareInstance] ;
}

-(id)copyWithZone:(struct _NSZone *)zone
{
    return [YMBaseTabBarViewController shareInstance] ;
}




-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    YMBaseNavViewController * nav = _contorll_array[3];
    if (nav.viewControllers.count >= 2)
    {
        scrollView.scrollEnabled = NO;
        
        return;
        
    }
    if (scrollView.contentOffset.x / MainSCreen.width < 0 || scrollView.contentOffset.x / MainSCreen.width > self.contorll_array.count - 1)
    {
        return;
    }
    
    for (UIButton *bt in _YMTabBar.subviews){
        
        if ([bt isKindOfClass:[UIButton class]])
        {
            bt.selected         = NO;
            bt.highlighted      = NO;
            bt.titleLabel.alpha = 0.4;
            [bt setUserInteractionEnabled:YES];
        }
        if (bt.tag == scrollView.contentOffset.x / MainSCreen.width + comTag)
        {
            
            bt.selected         = YES;
            bt.titleLabel.alpha = 1;
            [bt setUserInteractionEnabled:NO];
            
        }
    }

    
    
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
